﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using MulagoDb.model;
using System.Collections;
using MulagoDb.model_old.clinicvisit.tab2;
using MulagoDb.model_old;


namespace MulagoDb.database
{
    public class ClinicVisit
    {
        
        public static string insertClinicvisit(Clinicvisit visit)
        {
            try
            {
                PatientRegistration.persist.Save(visit);
                return "success";
            }
            catch (Exception ex) {
                return ex.Message;
            }
        }

        public static void insertClinicvisit_investigations(ClinicvisitInvestigations ci)
        {
            try
            {
                PatientRegistration.persist.Save(ci);
            }
            catch (Exception ex)
            {
               //
            }
        }


        public static void insertClinicvisit_majordiagnosis(ClinicvisitMajordiagnosis cm)
        {
            try
            {
                PatientRegistration.persist.Save(cm);
            }
            catch (Exception ex)
            {
             //
            }
        }


        public static void insertClinicvisit_measurements(ClinicvisitMeasurements cm)
        {
            try
            {
                PatientRegistration.persist.Save(cm);;
            }
            catch (Exception ex)
            {
             //
            }
        }


        public static void insertClinicvisit_physicalexamination(ClinicvisitPhysicalexamination cp)
        {
            try
            {
                PatientRegistration.persist.Save(cp); ;
            }
            catch (Exception ex)
            {
                //
            }
        }
        
        public static void insertClinicvisit_vitalsigns(ClinicvisitVitalsigns vs)
        {
            try
            {
                PatientRegistration.persist.Save(vs);
            }
            catch (Exception ex)
            {
                //
            }
        }

        //public static void insertDaycareTreatment(int pid,string treatmentGiven,string analgesia,string surgery,string others)
        //{
        //    MySqlConnection connection = new MySqlConnection(PatientRegistration.myConString);
        //    connection.Open();
        //    MySqlCommand command = connection.CreateCommand();
        //    string query = "INSERT INTO clinicvisit_vitalsigns VALUES(" + pid + ",now(),'" + treatmentGiven+ "','" + analgesia + "','" + surgery + "','" + others + "')";
        //    command.CommandText = query;
        //    MySqlDataReader reader = command.ExecuteReader();
        //    connection.Close();
        //}

        //tree view data
        public static void insertclinicvisit_febrileillness(ClinicvisitFebrileillness cf)
        {
            try
            {
                PatientRegistration.persist.Save(cf);
            }
            catch (Exception ex)
            {
                //
            }
        }

        public static void insertclinicvisit_dactylitis(ClinicvisitDactylitis cd)
        {
            try
            {
                PatientRegistration.persist.Save(cd);
            }
            catch (Exception ex)
            {
                //
            }
        }

        public static void insertclinicvisit_stroke(ClinicvisitStroke cs)
        {
            try
            {
                PatientRegistration.persist.Save(cs);
            }
            catch (Exception ex)
            {
                //
            }
        }

        public static void insertclinicvisit_acutepain(ClinicvisitAcutepain cva)
        {
            try
            {
                PatientRegistration.persist.Save(cva);
            }
            catch (Exception ex)
            {
                //
            }
        }

        public static void insertclinicvisit_avascularnecrosis(ClinicvisitAvascularnecrosis cava)
        {
            try
            {
                PatientRegistration.persist.Save(cava);
            }
            catch (Exception ex)
            {
                //
            }
        }

        // tab 2 
        public static void insertclinicvisit_daycare(ClinicvisitDaycare cd)
        {
            try
            {
                PatientRegistration.persist.Save(cd);
            }
            catch (Exception ex)
            {
                //
            }
        }
        //get daycare
        public static DayCareGeneral getclinicvisit_daycare(int pid,string date)
        {
            
                DayCareGeneral daycare = null ;
                MySqlConnection connection = new MySqlConnection(PatientRegistration.myConString);
            try{
                connection.Open();
                MySqlCommand command = connection.CreateCommand();
                string query = "SELECT *FROM clinicvisit_daycare where pid = " + pid + " and date = '" + date + "'";
                command.CommandText = query;
                MySqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    daycare = new DayCareGeneral();
                    daycare.setPid(reader.GetValue(0).ToString());
                    daycare.setDate(reader.GetValue(1).ToString());
                    daycare.setNodaycaregiven(reader.GetValue(2).ToString());
                    daycare.setIvfluids(reader.GetValue(3).ToString());
                    daycare.setAnalgesia(reader.GetValue(4).ToString());
                    daycare.setAntibiotics(reader.GetValue(5).ToString());
                    daycare.setOthers(reader.GetValue(6).ToString());

                }
                connection.Close();
            }
            catch (Exception ex) {
                Console.WriteLine(ex.Message);
                return null;
            }
            return daycare;
        }
        public static void insertClinicvisit_rbctranfusation(ClinicvisitRbctranfusation rbc)
        {
            try
            {
                PatientRegistration.persist.Save(rbc);
            }
            catch (Exception ex)
            {
                //
            }
        }
        //tab 3

        public static void insertclinicvisit_deposition(ClinicvisitDeposition cvdp)
        {

            try
            {
                PatientRegistration.persist.Save(cvdp);
            }
            catch (Exception ex)
            {
                //
            }
        }

        public static ArrayList searchPatientVist(string tobeSearchedID)
        {
            ArrayList pvisit = new ArrayList();
            MySqlConnection connection = new MySqlConnection(PatientRegistration.myConString);
            connection.Open();
            MySqlCommand command = connection.CreateCommand();

            command.CommandText = "select * from clinicvisit where pid like '%" + tobeSearchedID + "%'";

            MySqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                PatientVisit pat = new PatientVisit();
                pat.setpid(reader.GetValue(0).ToString());
                pat.setdate(reader.GetValue(1).ToString());
                pat.setReason(reader.GetValue(2).ToString());
                pvisit.Add(pat);
            }
            return pvisit;

        }

        public static ArrayList searchPatientVistDetails(string tobeSearchedID, string date, string tobeSearched)
        {
            ArrayList pvisitDetails = new ArrayList();
            MySqlConnection connection = new MySqlConnection(PatientRegistration.myConString);
            try
            {

                if (tobeSearched.Equals("vitalSigns"))
                {
                    connection.Open();
                    MySqlCommand command = connection.CreateCommand();
                    //SELECT * FROM clinicvisit_vitalsigns WHERE pid LIKE 29 AND date LIKE '2011-05-27'
                    command.CommandText = " SELECT * FROM clinicvisit_vitalsigns WHERE pid = " + tobeSearchedID + " AND date ='" + date + "' LIMIT 0 , 30";
                    MySqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        clinicvisit_vitalsigns pat = new clinicvisit_vitalsigns();
                        pat.setpid(reader.GetValue(0).ToString());
                        pat.setDate(reader.GetValue(1).ToString());
                        pat.sethr(reader.GetValue(2).ToString());
                        pat.setrr(reader.GetValue(3).ToString());
                        pat.setbp(reader.GetValue(4).ToString());
                        pat.settemperature(reader.GetValue(5).ToString());
                        pat.setsat(reader.GetValue(6).ToString());
                        pvisitDetails.Add(pat);

                    }
                    connection.Close();
                }
                else if (tobeSearched.Equals("measurement"))
                {

                    connection.Open();
                    MySqlCommand command = connection.CreateCommand();
                    command.CommandText = "SELECT * FROM clinicvisit_measurements WHERE pid = " + tobeSearchedID + " AND date ='" + date + "' LIMIT 0 , 30";
                    MySqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        clinicvisit_measurements pat = new clinicvisit_measurements();
                        pat.setpid(reader.GetValue(0).ToString());
                        pat.setDate(reader.GetValue(1).ToString());
                        pat.setheight(reader.GetValue(2).ToString());
                        pat.setweight(reader.GetValue(3).ToString());
                        pat.setleftmuac(reader.GetValue(4).ToString());
                        pvisitDetails.Add(pat);
                    }
                    connection.Close();
                }
                else if (tobeSearched.Equals("physicalexamination"))
                {
                    connection.Open();
                    MySqlCommand command = connection.CreateCommand();
                    command.CommandText = "SELECT * FROM clinicvisit_physicalexamination WHERE pid = " + tobeSearchedID + " AND date ='" + date + "' LIMIT 0 , 30";
                    MySqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        clinicvisit_physicalexamination pat = new clinicvisit_physicalexamination();
                        pat.setpid(reader.GetValue(0).ToString());
                        pat.setDate(reader.GetValue(1).ToString());
                        pat.sethistory(reader.GetValue(2).ToString());
                        pat.setpallor(reader.GetValue(3).ToString());
                        pat.setjuandice(reader.GetValue(4).ToString());
                        pat.setliver(reader.GetValue(5).ToString());
                        pat.setspleen(reader.GetValue(6).ToString());
                        pat.setother(reader.GetValue(7).ToString());
                        pvisitDetails.Add(pat);
                    }
                    connection.Close();
                }


                else if (tobeSearched.Equals("investigations"))
                {
                    connection.Open();
                    MySqlCommand command = connection.CreateCommand();
                    command.CommandText = "SELECT * FROM clinicvisit_investigations WHERE pid = " + tobeSearchedID + " AND date ='" + date + "' LIMIT 0 , 30";
                    MySqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        clinicvisit_investigations pat = new clinicvisit_investigations();
                        pat.setpid(reader.GetValue(0).ToString());
                        pat.setDate(reader.GetValue(1).ToString());
                        pat.sethb(reader.GetValue(2).ToString());
                        pat.setmcv(reader.GetValue(3).ToString());
                        pat.setwbc(reader.GetValue(4).ToString());
                        pat.setneutrophils(reader.GetValue(5).ToString());
                        pat.setplatelets(reader.GetValue(6).ToString());
                        pat.setmalaria(reader.GetValue(7).ToString());
                        pat.setreticulocytes(reader.GetValue(8).ToString());
                        pat.setbilirubinDirect(reader.GetValue(9).ToString());
                        pat.setbilirubinTotal(reader.GetValue(10).ToString());
                        pat.setother(reader.GetValue(11).ToString());

                        pvisitDetails.Add(pat);
                    }
                    connection.Close();
                }
                else if (tobeSearched.Equals("majordiagnosis"))
                {
                    connection.Open();
                    MySqlCommand command = connection.CreateCommand();
                    command.CommandText = "SELECT * FROM clinicvisit_majordiagnosis WHERE pid = " + tobeSearchedID + " AND date ='" + date + "' LIMIT 0 , 30";
                    MySqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        clinicvisit_majordiagnosis pat = new clinicvisit_majordiagnosis();
                        pat.setpid(reader.GetValue(0).ToString());
                        pat.setDate(reader.GetValue(1).ToString());
                        pat.setstablestate(reader.GetValue(2).ToString());
                        pat.setsplenicSquesation(reader.GetValue(3).ToString());
                        pat.setcholecystitisHeadache(reader.GetValue(4).ToString());
                        pat.setacutesyndrome(reader.GetValue(5).ToString());
                        pvisitDetails.Add(pat);
                    }
                    connection.Close();
                }

                else if (tobeSearched.Equals("febrileillness"))
                {
                    connection.Open();
                    MySqlCommand command = connection.CreateCommand();
                    command.CommandText = "SELECT * FROM clinicvisit_febrileillness WHERE pid = " + tobeSearchedID + " AND date ='" + date + "' LIMIT 0 , 30";
                    MySqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        clinicvisit_febrileillness pat = new clinicvisit_febrileillness();
                        pat.setpid(reader.GetValue(0).ToString());
                        pat.setDate(reader.GetValue(1).ToString());
                        pat.setprobableMalaria(reader.GetValue(2).ToString());
                        pat.settb_Suspected(reader.GetValue(3).ToString());
                        pat.setother_Respiratory(reader.GetValue(4).ToString());
                        pat.setgastroenteritis(reader.GetValue(5).ToString());
                        pat.setosteomyelitis(reader.GetValue(6).ToString());
                        pat.setprobable_sepsis(reader.GetValue(7).ToString());
                        pat.setno_specific_focus(reader.GetValue(8).ToString());
                        pat.setOtherspecify(reader.GetValue(9).ToString());
                        pvisitDetails.Add(pat);
                    }
                    connection.Close();
                }
                else if (tobeSearched.Equals("acutepain"))
                {
                    connection.Open();
                    MySqlCommand command = connection.CreateCommand();
                    command.CommandText = "SELECT * FROM clinicvisit_acutepain WHERE pid = " + tobeSearchedID + " AND date ='" + date + "' LIMIT 0 , 30";
                    MySqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {

                        clinicvisit_acutepain pat = new clinicvisit_acutepain();
                        pat.setpid(reader.GetValue(0).ToString());
                        pat.setDate(reader.GetValue(1).ToString());
                        pat.setThorax(reader.GetValue(2).ToString());
                        pat.setAbdomen(reader.GetValue(3).ToString());
                        pat.setBack(reader.GetValue(4).ToString());
                        pat.setHips(reader.GetValue(5).ToString());
                        pat.setLUpperLimb(reader.GetValue(6).ToString());
                        pat.setLLowerLimb(reader.GetValue(7).ToString());
                        pat.setRUpperLimb(reader.GetValue(8).ToString());
                        pat.setRLowerLimb(reader.GetValue(9).ToString());

                        pvisitDetails.Add(pat);
                    }
                    connection.Close();
                }
                else if (tobeSearched.Equals("dactylitis"))
                {
                    connection.Open();
                    MySqlCommand command = connection.CreateCommand();
                    command.CommandText = "SELECT * FROM clinicvisit_dactylitis WHERE pid = " + tobeSearchedID + " AND date ='" + date + "' LIMIT 0 , 30";
                    MySqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        clinicvisit_dactylitis pat = new clinicvisit_dactylitis();
                        pat.setpid(reader.GetValue(0).ToString());
                        pat.setDate(reader.GetValue(1).ToString());
                        pat.setRHand(reader.GetValue(2).ToString());
                        pat.setLHand(reader.GetValue(3).ToString());
                        pat.setRFoot(reader.GetValue(4).ToString());
                        pat.setLFoot(reader.GetValue(5).ToString());
                        pvisitDetails.Add(pat);
                    }
                    connection.Close();
                }

                else if (tobeSearched.Equals("avascularnecrosis"))
                {
                    connection.Open();
                    MySqlCommand command = connection.CreateCommand();
                    command.CommandText = "SELECT * FROM clinicvisit_avascularnecrosis WHERE pid = " + tobeSearchedID + " AND date ='" + date + "' LIMIT 0 , 30";
                    MySqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        clinicvisit_avascularnecrosis pat = new clinicvisit_avascularnecrosis();
                        pat.setpid(reader.GetValue(0).ToString());
                        pat.setDate(reader.GetValue(1).ToString());
                        pat.setRHumerus(reader.GetValue(2).ToString());
                        pat.setLhumerus(reader.GetValue(3).ToString());
                        pat.setRfemarolhead(reader.GetValue(4).ToString());
                        pat.setLfemoralhead(reader.GetValue(5).ToString());
                        pat.setother(reader.GetValue(6).ToString());
                        pvisitDetails.Add(pat);
                    }
                    connection.Close();
                }

                else if (tobeSearched.Equals("stroke"))
                {
                    connection.Open();
                    MySqlCommand command = connection.CreateCommand();
                    command.CommandText = "SELECT * FROM clinicvisit_stroke WHERE pid = " + tobeSearchedID + " AND date ='" + date + "' LIMIT 0 , 30";
                    MySqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        clinicvisit_stroke pat = new clinicvisit_stroke();
                        pat.setpid(reader.GetValue(0).ToString());
                        pat.setDate(reader.GetValue(1).ToString());
                        pat.setRUpperLimb(reader.GetValue(2).ToString());
                        pat.setRLowerLimb(reader.GetValue(3).ToString());
                        pat.setLUpperLimb(reader.GetValue(4).ToString());
                        pat.setLLowerLimb(reader.GetValue(5).ToString());
                        pat.setdysphasia(reader.GetValue(6).ToString());
                        pat.setcranialnerve(reader.GetValue(7).ToString());
                        pat.setother(reader.GetValue(8).ToString());
                        pvisitDetails.Add(pat);
                    }
                    connection.Close();
                }
            }
            catch (Exception ex) { 
            //
            }

            return pvisitDetails;
        }

    }
}
